﻿using Blazored.SessionStorage;
using System.Net.Http.Json;
using WhutDoomCheck.Shared.HttpModels;

namespace WhutDoomCheck.Client.Services
{
    public class SchoolClient
    {
        public SchoolClient(HttpClient http, ISessionStorageService cache)
        {
            this.http = http;
            this.cache = cache;
        }

        private readonly HttpClient http;
        private readonly ISessionStorageService cache;

        public async Task<bool> Login(string username, string password)
        {
            var resp = await http.PostAsJsonAsync("/api/login/school", new { username, password });

            if (resp.StatusCode == System.Net.HttpStatusCode.Unauthorized) return false;

            var result = await resp.Content.ReadFromJsonAsync<Token>();

            await cache.SetItemAsync("AccessToken", result!.AccessToken);
            return true;
        }

        public async Task<Stream> GetAllStudents()
        {
            var token = await cache.GetItemAsync<string>("AccessToken");

            http.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
            var result = await http.GetAsync("/Api/School/ExportAllStudents", HttpCompletionOption.ResponseContentRead);

            var stream = new MemoryStream();
            await result.Content.CopyToAsync(stream);
            stream.Position = 0;

            return stream;
        }

        public async Task<Stream> GetAllStudentsDivide()
        {
            var token = await cache.GetItemAsync<string>("AccessToken");

            http.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
            var result = await http.GetAsync("/Api/School/ExportAllStudentsDivideBySchool", HttpCompletionOption.ResponseContentRead);

            var stream = new MemoryStream();
            await result.Content.CopyToAsync(stream);
            stream.Position = 0;

            return stream;
        }

        public async Task<Stream> GetSchoolStatus()
        {
            var token = await cache.GetItemAsync<string>("AccessToken");

            http.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
            var result = await http.GetAsync("/Api/School/ExportSchoolStatus", HttpCompletionOption.ResponseContentRead);

            var stream = new MemoryStream();
            await result.Content.CopyToAsync(stream);
            stream.Position = 0;

            return stream;
        }

        public async Task<Stream> GetUnfinishedStudents()
        {
            var token = await cache.GetItemAsync<string>("AccessToken");

            http.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
            var result = await http.GetAsync("/Api/School/ExportUnfinishedStudents", HttpCompletionOption.ResponseContentRead);

            var stream = new MemoryStream();
            await result.Content.CopyToAsync(stream);
            stream.Position = 0;

            return stream;
        }

        public async Task<Stream> GetEmptyBeds()
        {
            var token = await cache.GetItemAsync<string>("AccessToken");

            http.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
            var result = await http.GetAsync("/Api/School/ExportEmptyBeds", HttpCompletionOption.ResponseContentRead);

            var stream = new MemoryStream();
            await result.Content.CopyToAsync(stream);
            stream.Position = 0;

            return stream;
        }
    }
}
